﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightSdms.VisitsService;
using System.Windows.Markup;
using System.Threading;
using SilverlightSdms.FacilitiesService;
using System.Windows.Data;

namespace SilverlightSdms.UserControls.Scheduler
{
    public partial class ChildWindowVisitManager : ChildWindow
    {
        public List<Visit> Visits { get; private set; }
        public DateTime Date { get; private set; }
        private Visit m_Visit;
        private VisitsServiceClient m_VisitClient = new VisitsServiceClient();
        private FacilitiesServiceClient m_FacilitiesClient = new FacilitiesServiceClient();
        private List<FacilitieInfo> m_Facilities;
        private bool m_IsEditMode = false;
        public int UserId { get; set; }

        public ChildWindowVisitManager()
        {
            InitializeComponent();

            UserId = ((App)Application.Current).UserId;
            this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
            txtBlockErrorMsg.Visibility = System.Windows.Visibility.Collapsed;
            //setting ServiceClient
            m_FacilitiesClient.GetFacilitiesByUserCompleted += (s, ea) => 
                {
                    m_Facilities = ea.Result.ToList();
                    comboBoxSchools.DataContext = m_Facilities;
                    comboBoxSchools.ItemsSource = m_Facilities;
                    comboBoxSchools.SelectedValuePath = "FacilitieId";
                    comboBoxSchools.DisplayMemberPath = "Name";
                    busyIndicator.IsBusy = false;
                };
            m_VisitClient.UpdateCompleted += (s, ea) =>
                {
                    if (ea.Error != null)
                    {
                        MessageBox.Show("Unable to update visit!");
                    }
                    else
                    {
                        MessageBox.Show("visit updated successfully!");
                    }
                    busyIndicator.IsBusy = false;
                };
            m_VisitClient.DeleteCompleted += (s, ea) =>
            {
                if (ea.Error != null)
                {
                    MessageBox.Show("Unable to delete visit!");
                }
                else
                {
                    MessageBox.Show("visit deleted successfully!");
                    Visits.Remove(m_Visit);
                    dataGridVisits.UpdateLayout();
                }
                busyIndicator.IsBusy = false;
            };
            m_VisitClient.AddCompleted += (s, ea) =>
            {
                if (ea.Error != null)
                {
                    MessageBox.Show("Unable to add visit!");
                }
                else
                {
                    MessageBox.Show("visit added successfully!");
                    Visits.Add(m_Visit);
                    dataGridVisits.UpdateLayout();
                }
                busyIndicator.IsBusy = false;
            };

        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        public void Init(List<Visit> Visits, DateTime Date)
        {
            this.Visits = Visits;
            this.Date = Date;
            dataGridVisits.DataContext = Visits;
            this.lblCurrnetMettings.DataContext = this;
        }

        private void btnGridRowEdit_Click(object sender, RoutedEventArgs e)
        {
            txtBlockErrorMsg.Visibility = System.Windows.Visibility.Collapsed;
            m_Visit = dataGridVisits.SelectedItem as Visit;
            btnAddOrUpdatte.Content = "Update";
            m_IsEditMode = true;
            comboBoxSchools.IsEnabled = false;
            btnCancelUpdatte.Visibility = System.Windows.Visibility.Visible;
            //
            //Binding binding = new Binding("IsDone");
            //binding.Source = m_Visit;
            //binding.Mode = BindingMode.TwoWay;
            //checkBoxIsDone.SetBinding(CheckBox.IsCheckedProperty, binding);
            //
            checkBoxIsDone.DataContext = m_Visit;
            checkBoxIsDone.IsChecked = m_Visit.IsDone;
            txtBoxDescription.Text = m_Visit.Description;
            timePikerEndTime.Value = m_Visit.EndTime;
            timePikerStartTime.Value = m_Visit.StartTime;
        }

        private void btnGridRowDelete_Click(object sender, RoutedEventArgs e)
        {
            busyIndicator.IsBusy = true;
            m_Visit = dataGridVisits.SelectedItem as Visit;
            m_Visit.IsDone = true;
            m_VisitClient.DeleteAsync(m_Visit);
        }

        private void btnGridRowComplete_Click(object sender, RoutedEventArgs e)
        {
            busyIndicator.IsBusy = true;
            m_Visit = dataGridVisits.SelectedItem as Visit;
            m_Visit.IsDone = true;
            m_VisitClient.UpdateAsync(m_Visit);
        }

        private void btnAddOrUpdatte_Click(object sender, RoutedEventArgs e)
        {
            txtBlockErrorMsg.Visibility = System.Windows.Visibility.Collapsed;
            if (string.IsNullOrEmpty(txtBoxDescription.Text))
            {
                txtBlockErrorMsg.Visibility = System.Windows.Visibility.Visible;
                txtBlockErrorMsg.Text = "Description is mandatory";
            }
            else if (timePikerEndTime.Value == null || timePikerStartTime.Value == null)
            {
                txtBlockErrorMsg.Visibility = System.Windows.Visibility.Visible;
                txtBlockErrorMsg.Text = "Start and end time are mandatory";
            }
            else if (timePikerEndTime.Value.Value < timePikerStartTime.Value.Value)
            {
                txtBlockErrorMsg.Visibility = System.Windows.Visibility.Visible;
                txtBlockErrorMsg.Text = "End time can not be before start time";
            }
            else
            {
                if (m_IsEditMode)
                {
                    busyIndicator.IsBusy = true;
                    m_Visit = dataGridVisits.SelectedItem as Visit;
                    m_Visit.IsDone = checkBoxIsDone.IsChecked.HasValue ? checkBoxIsDone.IsChecked.Value : false;
                    m_Visit.Description = txtBoxDescription.Text;
                    m_Visit.EndTime = timePikerEndTime.Value.Value;
                    m_Visit.StartTime = timePikerStartTime.Value.Value;
                    m_VisitClient.UpdateAsync(m_Visit);
                }
                else
                {
                    if (comboBoxSchools.SelectedIndex < 0)
                    {
                        txtBlockErrorMsg.Visibility = System.Windows.Visibility.Visible;
                        txtBlockErrorMsg.Text = "School is mandatory";
                    }
                    else
                    {
                        m_Visit = new Visit();
                        m_Visit.IsDone = checkBoxIsDone.IsChecked.HasValue ? checkBoxIsDone.IsChecked.Value : false;
                        m_Visit.Description = txtBoxDescription.Text;
                        m_Visit.EndTime = new DateTime(Date.Year,Date.Month,Date.Day,timePikerEndTime.Value.Value.Hour,
                            timePikerEndTime.Value.Value.Minute,timePikerEndTime.Value.Value.Second);
                        m_Visit.StartTime = new DateTime(Date.Year, Date.Month, Date.Day, timePikerStartTime.Value.Value.Hour,
                            timePikerStartTime.Value.Value.Minute, timePikerStartTime.Value.Value.Second);
                        m_VisitClient.AddAsync(m_Visit, ((App)Application.Current).UserId, ((FacilitieInfo)comboBoxSchools.SelectedItem).FacilitieId);
                    }
                }
            }
        }

        private void btnCancelUpdatte_Click(object sender, RoutedEventArgs e)
        {
            ResetControls();
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            busyIndicator.IsBusy = true;
            m_FacilitiesClient.GetFacilitiesByUserAsync(UserId);
        }

        private void ChildWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            ResetControls();
        }

        private void ResetControls()
        {
            btnAddOrUpdatte.Content = "Add";
            m_IsEditMode = false;
            comboBoxSchools.IsEnabled = true;
            btnCancelUpdatte.Visibility = System.Windows.Visibility.Collapsed;
            txtBoxDescription.Text = "";
            checkBoxIsDone.IsChecked = false;
            txtBlockErrorMsg.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}

